import java.awt.Color;
import java.awt.Point;
import java.util.ArrayList;

import pulpcore.animation.Easing;
import pulpcore.animation.Int;
import pulpcore.animation.event.RemoveSpriteEvent;
import pulpcore.image.BlendMode;
import pulpcore.image.Colors;
import pulpcore.image.CoreFont;
import pulpcore.image.CoreGraphics;
import pulpcore.image.CoreImage;
import pulpcore.image.filter.*;
import pulpcore.Input;
import pulpcore.scene.Scene2D;
import pulpcore.sprite.FilledSprite;
import pulpcore.sprite.Group;
import pulpcore.sprite.ImageSprite;
import pulpcore.sprite.Label;
import pulpcore.sprite.Sprite;
import pulpcore.Stage;
import static pulpcore.image.Colors.*;

public class FinalTest extends Scene2D {
    
    String[] tiles = {
        "Water Block.png",
        "Stone Block.png",
        "Grass Block.png",
        "Dirt Block.png",
    };
    
    String[] map = {
    	"WWWWWWWWWWWWWWWWWWWWWWWWW",
        "WGGGGSSSGGGGGGGGDWWWWDGGW",
        "WGGGGSSSGGGGGDDWWWWWDGGGW",
        "WGGGGSSSSSSSSSSSSSSSSSSSW",
        "WGGGGSSSSSSSSSSSSSSSSSSSW",
        "WGGGGSSSGGGGGDWWWWDGGGGGW",
        "WGGGGSSSGGGGGGDWWWWDGGGGW",
        "WGGGGSSSGGGGGDWWWWWDGGGGW",
        "WGGGGSSSGGGGDWWWWWDDGGGGW",
        "WGGGGSSSGGGGDWWWDGGGGGGGW",
        "WGGGGSSSGGGGDWWWDGGGGGGGW",
        "WSSSSSSSGGGGDWWWDGGGGGGGW",
        "WSSSSSSSGGGGDWWWDGGGGGGGW",
        "WGGGGSSSGGGGGDWWWDDGGGGGW",
        "WGGGGSSSSSSSSSSSSSSSSSSSW",
        "WGGGGSSSSSSSSSSSSSSSSSSSW",
        "WGGGGSSSSSSSSSSSSSSSSSSSW",
        "WGGGGGGGGGGGGGGGDWWWWWDGW",
        "WGGGGGGGGGGGGGGGGDDWWWWDW",
        "WGGGGGGGGGGGGGGGGGGDWWWWW",
        "WWWWWWWWWWWWWWWWWWWWWWWWW"
    };
    String[] map2 = {
         "W"
        };
    ImageSprite[][] grid = new ImageSprite[map[0].length()][map.length];
    Tile[][] tilegrid = new Tile[map[0].length()][map.length];
    TileMapSprite tileMap;
    ImageSprite cursor;
    Group mapSprites;

    Character selectedchar;
    int counter = 0;
    
    Label label;
    Label label2;
    Label hoverlabel;
    Label hoverlabel2;
    Label hitLabel;
    Label damageLabel;
    Label criticalLabel;	
    
    //Character selected = new Character;
    ImageSprite warrior = new ImageSprite("warrior1.png", Stage.getWidth()/2, 0);
    ImageSprite tank = new ImageSprite("tank1.png", Stage.getWidth()/2, 100);
    ImageSprite berserker = new ImageSprite("berserker1.png", Stage.getWidth()/2, 100);
    ImageSprite wyvern = new ImageSprite("wyvern1.png", Stage.getWidth()/2, 100);
    ImageSprite cavalier = new ImageSprite("cavalier1.png", Stage.getWidth()/2, 100);
    ImageSprite wizard = new ImageSprite("wizard1.png", Stage.getWidth()/2, 100);
    ImageSprite archer = new ImageSprite("archer1.png", Stage.getWidth()/2, 100);
    
   
    ImageSprite evilknight = new ImageSprite("knight1.png", Stage.getWidth()/2, 100);
    ImageSprite evilberserker = new ImageSprite("evilberserk1.png", Stage.getWidth()/2, 100);
    ImageSprite evilberserker2 = new ImageSprite("evilberserk1.png", Stage.getWidth()/2, 100);
   // ImageSprite player4 = new ImageSprite("warrior.png", Stage.getWidth()/2, 100);
    //Character selected = new Character;

    Role sword = new Role("Sword");
    Role axe = new Role("Axe");
    Role lance = new Role("Lance");
    Role magic = new Role("Magic");
    Role bowman = new Role("Bow");

   // public Character(String n, ImageSprite s, Role r, Point pos, int lvl, int hp, int atk, int def, int mve, int spd, int crt, int res, int dodge){
    Character Berserker = new Character("Berserker", berserker, axe, new Point(10,1) ,1, 40, 20, 10, 6, 10, 20, 9, 12);
    Character Tank = new Character("Tank", tank, lance, new Point(7,4) , 1, 60, 10, 30, 4, 1, 10, 5, 5);
    Character Warrior = new Character("Warrior", warrior, sword, new Point(8,6) , 1, 50, 15, 1, 5, 1, 15, 1, 10);
    Character WyvernKnight = new Character("Wyvern Knight", wyvern, lance, new Point(4,14) , 1, 35, 10, 1, 8, 1, 25, 5, 30);
    Character Cavalier = new Character("Cavalier", cavalier, sword, new Point(7,7) , 1, 45, 20, 10, 7, 1, 20, 1, 15);
    Character Wizard = new Character("Wizard", wizard, magic, new Point(5,2) , 1, 50, 15, 1, 5, 1, 0, 20, 10);
    Character Archer = new Character("Archer", archer, bowman, new Point(6,1) , 1, 20, 20, 5, 5, 10, 40, 20, 10);
   
    // Character Tank = new Character("Tank", player4, tank, new Point(3,4), 10, 35, 15, 10, 4, 3, 0, 5, 3);
    
    Character Enemy = new Character("Enemy Warrior", evilknight, sword, new Point(20,15), 1, 50, 15, 1, 5, 1, 15, 1, 10);
    Character Enemy2 = new Character("Enemy Berserker", evilberserker, axe, new Point(18,11)  ,1, 40, 20, 5, 6, 10, 20, 9, 12);
    Character Enemy3 = new Character("Enemy Berserker", evilberserker2, axe, new Point(23,9)  ,1, 40, 20, 5, 6, 10, 20, 9, 12);
    
    
    PlayerTeam team = new PlayerTeam();
    EnemyTeam team2 = new EnemyTeam();
    Point loc = new Point();
    
//>>>>>>> .r21
    @Override
    public void load() {
        // Add the background (sky-blue)
        add(new FilledSprite(rgb(100, 200, 255)));
        
        
        team.add(Warrior);
        team.add(Tank);
        team.add(Berserker);
        team.add(WyvernKnight);
        team.add(Wizard);
        team.add(Cavalier);
        team.add(Archer);
        
        team2.add(Enemy);
        team2.add(Enemy2);
        team2.add(Enemy3);
        // Add the tileset
        
        tileMap = createTileMapSprite(tiles, map2, 100, 80);
        
        add(tileMap);
        
        mapSprites = new Group();
        
        for (int i = 0; i < map.length; i++){
        	for (int j = 0; j < map[0].length(); j++)
        	{
        		if (map[i].charAt(j) == 'G')
        			grid[j][i] = new ImageSprite("Grass Block.png",j*100,i*80);
        		if (map[i].charAt(j) == 'S')
            		grid[j][i] = new ImageSprite("Stone Block.png",j*100,i*80);
        		if (map[i].charAt(j) == 'W')
            		grid[j][i] = new ImageSprite("Water Block.png",j*100,i*80);
        		if (map[i].charAt(j) == 'D')
            		grid[j][i] = new ImageSprite("Dirt Block.png",j*100,i*80);
        		mapSprites.add(grid[j][i]);
        		tilegrid[j][i] = new Tile(new Point(j,i), grid[j][i]);
        	}
        }        
        

        mapSprites.add(berserker);
        mapSprites.add(tank);
        mapSprites.add(warrior);
        mapSprites.add(wyvern);
        mapSprites.add(wizard);
        mapSprites.add(cavalier);
        mapSprites.add(evilknight);
        mapSprites.add(archer);
        
        mapSprites.add(evilberserker);
        mapSprites.add(evilberserker2);
        
        for (Character i : team.getTeam())
        {
        	i.getSprite().moveTo(i.getPositionX()*100,i.getPositionY()*80-80, 100);
        	tilegrid[i.getPositionX()][i.getPositionY()].setCharacter(i);
        }
        for (Character i : team2.getTeam())
        {
        	i.getSprite().moveTo(i.getPositionX()*100,i.getPositionY()*80-80, 100);
        	tilegrid[i.getPositionX()][i.getPositionY()].setCharacter(i);
        }
        
        
        // Add the cursor

        
        mapSprites.x.bindTo(tileMap.viewX);
        mapSprites.y.bindTo(tileMap.viewY);
        add(mapSprites);
        
        setCursor(Input.CURSOR_OFF);
        cursor = new ImageSprite("cursor.png", 0, 0);
        cursor.setBlendMode(BlendMode.Add());
        cursor.bindLocationTo(tileMap);
        add(cursor);
        
        label = new Label(CoreFont.getSystemFont().tint(BLACK),
                "Click on a player to select it", 100, Stage.getHeight()-50);
        add(label);
        
        label2 = new Label(CoreFont.getSystemFont().tint(BLACK),
                "", 100, Stage.getHeight()-25);
        add(label2);
        
        hoverlabel = new Label(CoreFont.getSystemFont().tint(BLACK),
                "", 600, Stage.getHeight()-50);
        add(hoverlabel);
        
        hoverlabel2 = new Label(CoreFont.getSystemFont().tint(BLACK),
                "", 600, Stage.getHeight()-25);
        add(hoverlabel2);
        
        hitLabel = new Label(CoreFont.getSystemFont().tint(BLACK),
                "", 975, 50);
        add(hitLabel);
        
        damageLabel = new Label(CoreFont.getSystemFont().tint(BLACK),
                "", 975, 100);
        add(damageLabel);
        
        criticalLabel = new Label(CoreFont.getSystemFont().tint(BLACK),
                "", 975, 150);
        add(criticalLabel);
    }
    
    @Override
    public void update(int elapsedTime) {
    	
        	
        cursor.setLocation(Input.getMouseX(), Input.getMouseY());
        if (selectedchar != null){
        	
        	for (int i = loc.y-selectedchar.getMove()-selectedchar.getRange(); i <= loc.y+selectedchar.getMove()+selectedchar.getRange(); i++){
	        	for (int j = loc.x-selectedchar.getMove()-selectedchar.getRange(); j <= loc.x+selectedchar.getMove()+selectedchar.getRange(); j++)
	        	{
	        		if (i > -1 && i < map.length && j > -1 && j < map[0].length())
	        		{
	        			if (Math.abs(loc.y-i)+Math.abs(loc.x-j) < selectedchar.getMove()+selectedchar.getRange() && (!(map[i].charAt(j) == 'W') || selectedchar.getName().equals("Wyvern Knight")))
        				{	     
	        				tilegrid[j][i].setSuperSpecial();
	        				if (tilegrid[j][i].getCharacter() != null)
	        				{
	        					if (!selectedchar.canAttack(tilegrid[j][i].getCharacter()))
	        					{
	        						tilegrid[j][i].unSuperSpecial();
	        					}
	        				}
        					
        				}
	        		}
	        	}
	        }
	        for (int i = loc.y-selectedchar.getMove(); i <= loc.y+selectedchar.getMove(); i++){
	        	for (int j = loc.x-selectedchar.getMove(); j <= loc.x+selectedchar.getMove(); j++)
	        	{
	        		if (i > -1 && i < map.length && j > -1 && j < map[0].length())
	        		{
	        			if (Math.abs(loc.y-i)+Math.abs(loc.x-j) < selectedchar.getMove())
        				{	     
        					if (tilegrid[j][i].getCharacter() == null && (!(map[i].charAt(j) == 'W') || selectedchar.getName().equals("Wyvern Knight")))
        					{
        						tilegrid[j][i].setSpecial();
        					}
        					
        				}
	        		}
	        	}
	        }
	        
        }
        if (Input.isMousePressed())
        {
            int x = Input.getMousePressX();
            int y = Input.getMousePressY();
            //move character
            
            if(selectedchar != null && team2.isClicked(selectedchar, tilegrid) != null){
            	Character enemy = team2.isClicked(selectedchar, tilegrid);
            	if(selectedchar.canAttack(enemy)){
            		selectedchar.attackPlan(enemy);
            		if (Math.abs(selectedchar.getPositionX() - enemy.getPositionX())+Math.abs(selectedchar.getPositionY() - enemy.getPositionY()) <= selectedchar.getRange())
    				{
	            		if(Input.isAltDown() && tilegrid[enemy.getPositionX()][enemy.getPositionY()].getSuperSpecial()){	
	            			selectedchar.battle(enemy);
	            			selectedchar.justActed(true);
	            			selectedchar.unSelected();
	            			selectedchar.getSprite().setFilter(null);
	            			clearTileGrid();
	            			selectedchar = null;
	            		}
    				}
            	}
            }
            else if (selectedchar != null && team.isClicked(selectedchar, tilegrid) == null )
            {
                    for (int i = 0; i < map.length; i++){
                    	for (int j = 0; j < map[0].length(); j++)
                    	{
                    		if (selectedchar != null)
                            {
    		                	if (tilegrid[j][i].getSprite().isMousePressed()){
    		                		if (tilegrid[j][i].getSpecial())
    		                    	{
    		                         	tilegrid[selectedchar.getPositionX()][selectedchar.getPositionY()].setCharacter(null);
    			                		selectedchar.setPosition(j,i);
    			                		//tilegrid[j][i].setCharacter(selectedchar);
    			                		selectedchar.getSprite().moveTo(j*100, i*80-80, 200);
    			                		//selectedchar = null;
    		                    	}
    		                		else if (tilegrid[j][i].getCharacter() != null && !tilegrid[j][i].getCharacter().getActed())
    		                		{
    		                			tilegrid[selectedchar.getPositionX()][selectedchar.getPositionY()].setCharacter(null);
    		                            //tilegrid[loc.x][loc.y].setCharacter(selectedchar);
    		                			selectedchar.getSprite().moveTo(loc.x*100, loc.y*80-80, 200);
    		                        	selectedchar.unSelected();
    		                        	loc = new Point(j,i);
    		                			selectedchar = tilegrid[j][i].getCharacter();
    		                		}
    			                }
                            }
                    	}
                    }
               
                
            } 
           
        }

        if(Input.isDown(Input.KEY_SPACE)&& selectedchar!=null){
            clearTileGrid();
        	selectedchar.justActed(true);
        	selectedchar.unSelected();
        	selectedchar = null;
        	
        }
        if(Input.isReleased((Input.KEY_ENTER)))
        {
            clearTileGrid();
            if (selectedchar != null)
            {
            	loc = new Point(selectedchar.getPositionX(), selectedchar.getPositionY());
            	selectedchar.unSelected();
            	selectedchar = null;
            }
        	 for(Character i: team.getTeam()){
        		 i.justActed(true);
        	 }
        	
        }
       
	    
        
        //set sum labels
        if (selectedchar != null)
    	{
    		label.setText(selectedchar.toString());
    		label2.setText(selectedchar.toString2());
    	}
    	else if(!label.getText().equals("Click on a player to select it"))
    	{
    		label.setText("");
    		label2.setText("");
    	}

       //GOES THROUGH MAP
	        for (int i = 0; i < map.length; i++){
	        	for (int j = 0; j < map[0].length(); j++)
	        	{
	        		if (selectedchar != null)
	                {
	                	if (tilegrid[j][i].getSprite().isMouseHover())
	                	{
	                		if (tilegrid[j][i].getCharacter() != null){
	                			hoverlabel2.setText(tilegrid[j][i].getCharacter().toString2());
	                			hoverlabel.setText(tilegrid[j][i].getCharacter().toString());
	                			if(!selectedchar.getTeam().equals(tilegrid[j][i].getCharacter().getTeam())){
	                				hitLabel.setText("Hit Chance:"+selectedchar.attackPlan(tilegrid[j][i].getCharacter())[0]);
	        	            		damageLabel.setText("Damage:"+selectedchar.attackPlan(tilegrid[j][i].getCharacter())[1]);
	        	             		criticalLabel.setText("Crit Chance:"+selectedchar.attackPlan(tilegrid[j][i].getCharacter())[2]);
	                			}else{
	                				hitLabel.setText("");
	        	            		damageLabel.setText("");
	        	             		criticalLabel.setText("");
	                			}
	                			
	                		}else{
		                		hoverlabel.setText("");
	                			hoverlabel2.setText("");
	                			hitLabel.setText("");
	    	            		damageLabel.setText("");
	    	             		criticalLabel.setText("");
	
	                		}
	                	}
	                }
	        	
	        
	        		 //SETS OVERLAPPING OF CHARACTERS
	        		if (tilegrid[j][i].getCharacter() != null && tilegrid[j][i].getCharacter().getSprite().getParent() != null)
	        		{
	        			tilegrid[j][i].getCharacter().getSprite().getParent().moveToTop(tilegrid[j][i].getCharacter().getSprite());
	        		}
	        		//SETS BLUE MOVE FILTER
	        		if  (tilegrid[j][i].getSpecial())
	        		{
	        			tilegrid[j][i].getSprite().setFilter(new ColorOverlay(Colors.BLUE,100));
	        		}
	        		else
	        		{
	        			tilegrid[j][i].getSprite().setFilter(null);
	        		}
	        		//SETS RED ATTACK FILTER
	        		if (tilegrid[j][i].getSuperSpecial() && !tilegrid[j][i].getSpecial())
	        		{
	        			tilegrid[j][i].getSprite().setFilter(new ColorOverlay(Colors.RED,100));
	        		}
	        	}
	        
	        }
        
        
        if(Input.isDown(Input.KEY_UP)||Input.isDown(Input.KEY_W)){
    		int goalY = tileMap.viewY.get() + (Stage.getHeight() /2)/2;
    		tileMap.viewY.animateTo(goalY, 500, Easing.REGULAR_OUT);
    	}
    	
    	if(Input.isDown(Input.KEY_DOWN)||Input.isDown(Input.KEY_S)){
    		int goalY = tileMap.viewY.get() - (Stage.getHeight() /2)/2;
    		tileMap.viewY.animateTo(goalY, 500, Easing.REGULAR_OUT);
                     
	    }
		
		if(Input.isDown(Input.KEY_RIGHT)||Input.isDown(Input.KEY_D)){
			int goalx = tileMap.viewX.get() - (Stage.getHeight() /2)/2;
			tileMap.viewX.animateTo(goalx, 500, Easing.REGULAR_OUT);
		}
		
		if(Input.isDown(Input.KEY_LEFT)||Input.isDown(Input.KEY_A)){
			int goalx = tileMap.viewX.get() + (Stage.getHeight() /2)/2;
			tileMap.viewX.animateTo(goalx, 500, Easing.REGULAR_OUT);
		}        	

        //characters in your team
        for(int x=0; x<team.getTeam().size(); x++){
        	Character i = team.getTeam().get(x);
            //set characters to grid
        	tilegrid[i.getPositionX()][i.getPositionY()].setCharacter(i);
        	
        	//set filters
        	i.getSprite().setFilter(null);
        	if (i.getActed())
        	{
        		i.getSprite().setFilter(new Grayscale());
    			if (i.getName().equals("Tank"))
    			{
    				i.getSprite().setImage("tank1.png");
    			}
        	}
        	
        	if (i.selected(tilegrid) != null){
        		if (selectedchar != null)
        		{
        			selectedchar.getSprite().setFilter(null);
        		}
        		else
        		{
        			if (i.getName().equals("Tank"))
        			{
        				i.getSprite().setImage("tank2.png");
        			}
            		selectedchar = i;
        			loc = new Point(i.getPositionX(), i.getPositionY());
        		}
        	}
        	
            //check if dead and remove from team
        	if(i.isDead()){
        		tilegrid[i.getPositionX()][i.getPositionY()].setCharacter(null);
        		RemoveSpriteEvent die = new RemoveSpriteEvent(mapSprites, i.getSprite(), 10);
        		team.remove(i);
        		x--;
        	}
        }
        //characters in enemy team
        for(int x=0; x<team2.getTeam().size(); x++){
        	Character i = team2.getTeam().get(x);
        	tilegrid[i.getPositionX()][i.getPositionY()].setCharacter(i);
        	
        	if(i.isDead()){
        		tilegrid[i.getPositionX()][i.getPositionY()].setCharacter(null);
        		RemoveSpriteEvent die = new RemoveSpriteEvent(mapSprites, i.getSprite(), 10);
        		team2.remove(i);
        		clearTileGrid();
        		x--;
        	}
        }
        
        if(team.isTurnDone()){
        	Character enemy = team2.getTeam().get(counter);
        	ArrayList<Character> targets = enemy.getTargets(team, team2, map);
        	if(targets.size()!=0){
        		Character target = enemy.chooseTarget(targets, team, team2, map);
					enemy.setPosition(enemy.getAdjacentLocations(target.getPosition(), team, team2, map).get(0));
		        	System.out.println(enemy.getSprite().y.getAsInt());
		        	System.out.println(enemy.getPositionY()*80-80);
					if ((enemy.getSprite().x.getAsInt() <= enemy.getPositionX()*100+5) && 
							(enemy.getSprite().x.getAsInt() >= enemy.getPositionX()*100-5) && 
							(enemy.getSprite().y.getAsInt() <= enemy.getPositionY()*100-75) && 
							(enemy.getSprite().y.getAsInt() >= enemy.getPositionY()*80-85))
		        	{
		        		enemy.battle(target);
		              	counter++;
		              	System.out.println("Counter up!");
		              	enemy.getSprite().setFilter(new Grayscale());
		        	}			
					if(enemy.isDead()){
						counter--;
					}
        	}else{
        		counter++;
        		System.out.println("no targets");
        		enemy.getSprite().setFilter(new Grayscale());
        	}
        }
        
        if(counter>=team2.getTeam().size()){
        	team.resetTurn();
        	counter = 0;
        	for(Character i : team2.getTeam()){
        		i.getSprite().setFilter(null);
        	}
        }
        
        if(team.getTeam().size()==0){
        	
        }
        
        
        
        
        // When scrolling, disable dirty rectangles and hide the cursor
        setDirtyRectanglesEnabled(!tileMap.isScrolling());
        cursor.visible.set(Input.isMouseInside() && !tileMap.isScrolling());
      }
        
    public void clearTileGrid()
    {
        for (int i = 0; i < map.length; i++){
        	for (int j = 0; j < map[0].length(); j++)
        	{
        		tilegrid[j][i].unSpecial();
        		tilegrid[j][i].unSuperSpecial();
        			
        	}
        }
    }
    
    TileMapSprite createTileMapSprite(String[] tiles, String[] map, int tileWidth, int tileHeight) {
        // Load tile images
        CoreImage[] tileImages = new CoreImage[tiles.length];
        for (int i = 0; i < tiles.length; i++) {
            tileImages[i] = CoreImage.load(tiles[i]);
        }
        
        // Create the tile map
        int mapWidth = map[0].length();
        int mapHeight = map.length;
        CoreImage[][] tileMapImages = new CoreImage[mapWidth][mapHeight];
        for (int i = 0; i < mapWidth; i++) {
            for (int j = 0; j < mapHeight; j++) {
                
                // Convert the map char to the first letter of the tile name
                // i.e., 'W' = "Water Block.png"
                char ch = map[j].charAt(i);
                int index = 0;
                for (int k = 0; k < tiles.length; k++) {
                    if (tiles[k].charAt(0) == ch) {
                        index = k;
                        break;
                    }
                }
                tileMapImages[i][j] = tileImages[index];
            }
        }
        return new TileMapSprite(tileMapImages, tileWidth, tileHeight);
    }
    
    /**
        A simple tile map.
        Limitation: the maximum width and height of a TileMap 
        (i.e. tileWidth*numTilesAcross and tileHeight*numTilesDown) should be less than 32768.
    */

    static class TileMapSprite extends Sprite{
        
        private CoreImage[][] tileMap;
        private int tileWidth;
        private int tileHeight;
        private int numTilesAcross;
        private int numTilesDown;
        
       
        public final Int viewX = new Int(this);
        public final Int viewY = new Int(this);
        
        public TileMapSprite(CoreImage[][] tileMap, int tileWidth, int tileHeight) {
            super(0, 0, Stage.getWidth(), Stage.getHeight());
            this.tileMap = tileMap;
            this.tileWidth = tileWidth;
            this.tileHeight = tileHeight;
            numTilesAcross = tileMap.length;
            numTilesDown = tileMap[0].length;
        }
        
        public CoreImage getCoreImage(int x, int y){
        	return tileMap[x][y];
        }
        public int getMapWidth() {
            return tileWidth * numTilesAcross;
        }
        
        public int getMapHeight() {
            return tileHeight * numTilesDown;
        }
        
        public boolean isScrolling() {
            return viewX.isAnimating() || viewY.isAnimating();
        }
        
        @Override 
        public void update(int elapsedTime) {
        	
            super.update(elapsedTime);
            viewX.update(elapsedTime);
            viewY.update(elapsedTime);
           
           
            
        }
        
        public void flip(int x, int y){
        	CoreImage evan = getCoreImage(x, y);
            evan.flip();
        }
        
        @Override
        protected void drawSprite(CoreGraphics g) {
            int y = viewY.get();
            for (int j = 0; j < numTilesDown; j++) {
                int x = viewX.get();
                for (int i = 0; i < numTilesAcross; i++) {
                    g.drawImage(tileMap[i][j], x, y);
                    x += tileWidth;
                }
                y += tileHeight;
            }
        }
    }
}